home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / overview / dave falkenburg's sprocket / sprocketsample.cp < prev    next >
Encoding:
Text File  |  2000-09-28  |  7.5 KB  |  335 lines

  1. /*
  2.     File:        SprocketSample.cp
  3.  
  4.     Contains:    Boilerplate application-specific code.
  5.  
  6.     Written by: Dave Falkenburg    
  7.  
  8.     Copyright:    Copyright © 1993-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 8/19/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 12/6/94        DRF                Gave this file a better name. Also stop defining kCreditsBox
  21.                                             here.
  22.                  11/8/94        DRF                (LDR) Modified to pass all non-standard menu selections through
  23.                                             to window obj.
  24.                 11/8/94        DRF                DoEditMenu is gone; call DoMenuSelection instead.
  25.                 9/28/94        DRF                Changes for Dave Mark: More comments & rearranged code.
  26.                  9/27/94        DRF                Changes for Dave Mark: AppLib.h is now Sprocket.h &
  27.                                              clipboard conversion routines are fixed.
  28.                 9/9/94        DRF                Reorganized headers and removed redundant #includes and
  29.                                             conditionalized AOCE support.
  30.  
  31. */
  32. #include "Sprocket.h"
  33. #include "Window.h"
  34. #include "StandardMenus.h"
  35. #include "SplashWindow.h"
  36.  
  37. #include <Desk.h>
  38. #include <Resources.h>
  39. #include <Errors.h>
  40.  
  41. #include "ToolWindow.h"
  42. #include "DocWindow.h"
  43. #include "PreferencesDialogWindow.h"
  44. #include "MailableDocWindow.h"
  45.  
  46. AppPreferences    gPreferences;
  47.  
  48. //    Function Prototypes:
  49.  
  50. void    AboutBox(void);
  51. void    OpenExistingDocument(void);
  52.  
  53.  
  54. #define    mDebug                            256
  55. #define        iNewMailableWindow            1
  56.  
  57. #define    kAboutBoxFor68K                    256
  58. #define    kAboutBoxForPowerPC                257
  59. #define        iCreditsButton                2
  60.  
  61.  
  62. ////////////////////////////////////////////////////////////////////
  63. //
  64. //    SetupApplication
  65. //
  66. //    Modify this function to provide any application-specific
  67. //    initialization you may require.
  68. //
  69. //    This is a good place to call EnterMovies, allocate
  70. //    sound channels, setup network things, etc.
  71.  
  72. OSErr
  73. SetupApplication(void)
  74.     {    
  75.     InsertMenu(GetMenu(mDebug),0);                //    Add Debug Menu
  76.  
  77. #if    qAOCEAware
  78.     if (gHasAOCE)
  79.         EnableItem(GetMHandle(mDebug),1);
  80. #endif
  81.  
  82.     TToolWindow    * someTools = new TToolWindow(1025);
  83.  
  84.     gPreferences.fMailPreferences.fCreateMailerForNewDocuments = true;
  85.  
  86.     InitCursor();
  87.     return (noErr);
  88.     }
  89.  
  90.  
  91. ////////////////////////////////////////////////////////////////////
  92. //
  93. //    TearDownApplication
  94. //
  95. //    Modify this function to tear down anything that you allocated
  96. //    from within SetupApplication.
  97.  
  98. void
  99. TearDownApplication(void)
  100.     {
  101.     }
  102.  
  103.  
  104. ////////////////////////////////////////////////////////////////////
  105. //
  106. //    HandleMenu
  107. //
  108. //    Modify this function to handle any menu commands you might add
  109. //    to Sprocket’s default menubar. This isn’t the greatest strategy,
  110. //    but works for most things.
  111.  
  112. void
  113. HandleMenu(TWindow * topWindowObj, long menuCode)
  114.     {
  115.     short    menu = (short) (menuCode >> 16);
  116.     short    item = (short) (menuCode & 0xffff);
  117.     Str255    deskAccName;
  118.     
  119.     switch (menu)
  120.         {
  121.         case    mApple:
  122.             switch (item)
  123.                 {
  124.                 case    iAbout:
  125.                     AboutBox();
  126.                     break;
  127.                     
  128.                 default:
  129.                     GetMenuItemText(GetMenuHandle(mApple),item,deskAccName);
  130.                     (void) OpenDeskAcc(deskAccName);
  131.                     break;
  132.                 }
  133.             break;
  134.             
  135.         case    mFile:
  136.             switch (item)
  137.                 {
  138.                 case    iNew:
  139.                     CreateNewDocument();
  140.                     break;
  141.                 
  142.                 case    iOpen:
  143.                     OpenExistingDocument();
  144.                     break;
  145.                 
  146.                 case    iClose:
  147.                     HandleClose(MyFrontNonFloatingWindow());
  148.                     break;
  149.                     
  150.                 case    iPreferences:
  151.                     TPreferencesDialogWindow * prefsDialog = new TPreferencesDialogWindow;
  152.                     break;
  153.  
  154.                 case    iQuit:
  155.                     gDone = true;
  156.                     break;
  157.                     
  158.                 default:
  159.                     break;
  160.                 }
  161.             break;
  162.         
  163.         case    mEdit:
  164.             switch (item)
  165.                 {
  166.                 case    iShowOrHideClipboard:
  167.                     break;
  168.                     
  169.                 default:
  170.                     if ((!SystemEdit(item-1)) && (topWindowObj != nil))
  171.                         topWindowObj->DoMenuSelection(menu,item);
  172.                     break;
  173.                 }
  174.         
  175.         case    mDebug:
  176.             switch(item)
  177.                 {
  178.                 case    iNewMailableWindow:
  179. #if    qAOCEAware
  180.                     TMailableDocWindow *aWackyThing = new TMailableDocWindow;
  181. #endif
  182.                     break;
  183.                     
  184.                 default:
  185.                     break;
  186.                 }
  187.             break;
  188.             
  189.         default:
  190.             if (topWindowObj != nil)
  191.                 topWindowObj->DoMenuSelection(menu, item);
  192.             break;
  193.         }
  194.     }
  195.  
  196.  
  197. ////////////////////////////////////////////////////////////////////
  198. //
  199. //    ReadLocalClipboardFromScrap & WriteLocalClipboardToScrap
  200. //
  201. //    These functions will be called whenever the user switches into
  202. //    or out of your application.
  203. //
  204. //    If you keep copy of the clipboard in your own data structures,
  205. //    these functions will allow you to keep it in synch so that the
  206. //    user can cut and paste information between your program and
  207. //    other applications.
  208. //
  209. //    We don’t actually use the clipboard in Sprocket, so these
  210. //    functions are empty for now.
  211.  
  212. void
  213. ReadLocalClipboardFromScrap(void)
  214.     {
  215.     }
  216.  
  217. void
  218. WriteLocalClipboardToScrap(void)
  219.     {
  220.     }
  221.  
  222.  
  223. ////////////////////////////////////////////////////////////////////
  224. //
  225. //    CreateNewDocument, OpenDocument, PrintDocument, and
  226. //    QuitApplication.
  227. //
  228. //    These functions will be called whenever one of the required
  229. //    AppleEvents is sent to your application— either when your
  230. //    program is launched, or when the user opens a document from
  231. //    the Finder.
  232.  
  233. OSErr
  234. CreateNewDocument(void)
  235.     {
  236.     TDocWindow    *aNewWindow = new TDocWindow();
  237.     
  238.     if (aNewWindow)
  239.         return noErr;
  240.     else
  241.         return memFullErr;
  242.     }
  243.  
  244.  
  245. OSErr
  246. OpenDocument(LetterDescriptor * /* theDocument */, void * /*unused*/)
  247.     {
  248.     //    Normally, you would open the file (or letter) and use the
  249.     //    information to create windows, etc.
  250.     
  251.     //    For now, we just create a new document.
  252.  
  253.     return CreateNewDocument();
  254.     }
  255.  
  256.  
  257. OSErr
  258. PrintDocument(LetterDescriptor * /* theDocument */, void * /*unused*/)
  259.     {
  260.     //    We don’t do printing yet, but you could.
  261.     return errAEEventNotHandled;
  262.     }
  263.  
  264.  
  265. Boolean
  266. QuitApplication(void)
  267.     {
  268.     //    Just go ahead and say we’re done quitting…
  269.     
  270.     //    Normally, you would offer to close any unsaved documents,
  271.     //    allowing the user to cancel along the way.
  272.  
  273.     return true;
  274.     }
  275.  
  276.  
  277.  
  278. ////////////////////////////////////////////////////////////////////
  279. //
  280. //    AboutBox
  281. //
  282. //    Put up our about box, including the version number. Depending
  283. //    on what version we are running, pick the correct DLOG resource.
  284. //
  285.  
  286. void
  287. AboutBox(void)
  288.     {
  289.     Handle            versionHandle;
  290.     StringPtr        nullStr = (StringPtr) "\p";
  291.     StringPtr        shortVersionString = nullStr;
  292.     short            itemHit;
  293.     
  294.     versionHandle = GetResource('vers',1);
  295.     if (versionHandle)
  296.         shortVersionString = (StringPtr) ((char *) *versionHandle + 6);
  297.     ParamText(shortVersionString,nullStr,nullStr,nullStr);
  298.     ReleaseResource(versionHandle);
  299.  
  300. #ifndef    powerc
  301.     itemHit = StandardAlert(kAboutBoxFor68K);
  302. #else
  303.     itemHit = StandardAlert(kAboutBoxForPowerPC);
  304. #endif
  305.  
  306.     if (itemHit == iCreditsButton)
  307.         StandardAlert(kCreditsBox);
  308.     }
  309.  
  310.  
  311.  
  312. ////////////////////////////////////////////////////////////////////
  313. //
  314. //    OpenExistingDocument
  315. //
  316. //    Use StandardFile to ask the user for a file to open. We use
  317. //    CustomGetFile so that we can properly handle update events
  318. //    in other windows while the dialog is active.
  319.  
  320. void
  321. OpenExistingDocument(void)
  322.     {
  323.     StandardFileReply    reply;
  324.     SFTypeList            ourTypes;
  325.     
  326.     Point    where = { -1, -1 };
  327.     
  328.     HiliteWindowsForModalDialog(false);
  329.     CustomGetFile((FileFilterYDUPP) nil, -1, ourTypes, &reply, 0, where,
  330.                   (DlgHookYDUPP) nil,StandardDialogFilterYD, nil,nil,nil);
  331.     HiliteWindowsForModalDialog(true);
  332.     }
  333.  
  334.  
  335.